package com.itheima.web.service.impl.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.pms.PmsBrandService;
import com.itheima.vo.pms.PmsBrand;
import com.itheima.web.mapper.pms.PmsBrandMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {
    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    @Override
    public ResultData insert(PmsBrand pmsBrand) {
        this.save(pmsBrand);
        return ResultData.ok("","");
    }

    //批量删除
    @Override
    public ResultData batch(List<Integer> ids) {
        LambdaQueryWrapper<PmsBrand> lqw=new LambdaQueryWrapper();
        lqw.in(PmsBrand::getId,ids);
        this.remove(lqw);
        return ResultData.ok("操作成功",ids.size());
    }

    @Override
    public ResultData listPage(String keyword, Integer pageNum, Integer pageSize) {
        //构造分页构造器
        Page<PmsBrand> infoPage=new Page<>(pageNum,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<PmsBrand> lqw = new LambdaQueryWrapper<>();
        if (keyword!=null) {
            lqw.like(PmsBrand::getName, keyword);
        }
        this.page(infoPage, lqw);
        return ResultData.ok("操作成功", PageResult.init(infoPage.getTotal(),pageNum.longValue(),pageSize.longValue(),infoPage.getRecords()));
    }

    @Override
    public void updateFactoryStatus(List<Integer> ids, Integer factoryStatus) {
        //条件构造器
        LambdaQueryWrapper<PmsBrand> lqw=new LambdaQueryWrapper<>();
        lqw.in(PmsBrand::getId,ids);
        List<PmsBrand> list = this.list(lqw);
        for (PmsBrand pmsBrand : list) {
            pmsBrand.setFactoryStatus(factoryStatus);
            this.updateById(pmsBrand);
        }
    }

    @Override
    public void updateShowStatus(List<Integer> ids, Integer showStatus) {
        //条件构造器
        LambdaQueryWrapper<PmsBrand> lqw=new LambdaQueryWrapper<>();
        lqw.in(PmsBrand::getId,ids);
        List<PmsBrand> list = this.list(lqw);
        for (PmsBrand pmsBrand : list) {
            pmsBrand.setShowStatus(showStatus);
            this.updateById(pmsBrand);
        }
    }
}
